<template>
  <div class="app-container">
    <el-button type="success" size="mini" @click="showAddDialog()"
      >添加顶级菜单</el-button
    >
    <el-table
      :data="permissionVOList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column prop="icon" label="图标">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="权限类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 0">目录权限</el-tag>
          <el-tag v-if="scope.row.type == 1" type="success">菜单权限</el-tag>
          <el-tag v-if="scope.row.type == 2" type="warning">按钮权限</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="path" label="路由地址"> </el-table-column>
      <el-table-column prop="component" label="组件路径"> </el-table-column>
      <el-table-column prop="permissionValue" label="权限值"> </el-table-column>
      <el-table-column label="操作" width="180" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-plus"
            title="添加下级菜单"
            @click="showAddDialog(scope.row)"
          ></el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            title="修改"
            @click="showUpdateDialog(scope.row)"
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            title="删除"
            @click="deleteById(scope.row.id)"
            :disabled="scope.row.children.length > 0"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加、修改的dialog -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <el-form ref="form" :model="permission" label-width="150px">
        <el-form-item label="上级权限">
          <el-input v-model="permission.parentName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="权限类型">
          <el-radio-group v-model="permission.type" :disabled="typeDisabled">
            <el-radio :label="0" :disabled="type0Disabled">目录</el-radio>
            <el-radio :label="1" :disabled="type1Disabled">菜单</el-radio>
            <el-radio :label="2" :disabled="type2Disabled">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="权限名字">
          <el-input v-model="permission.name"></el-input>
        </el-form-item>
        <el-form-item label="图标" v-if="permission.type != 2">
          <el-select v-model="permission.icon" clearable>
            <el-option
              v-for="item in iconList"
              :key="item.class"
              :label="item.class"
              :value="item.class"
            >
              <span style="float: left">
                <i :class="item.class"></i>
              </span>
              <span style="padding-left: 6px">{{ item.class }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="permission.sort"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item v-if="permission.type !== 2">
          <span slot="label">
            <el-tooltip content="访问的路由地址，如：`rbac`" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            路由地址
          </span>
          <el-input v-model="permission.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item v-if="permission.type !== 0 && permission.type !== 2">
          <span slot="label">
            <el-tooltip
              content="访问的组件路径，如：`rbac/user/list`，默认在`views`目录下"
              placement="top"
            >
              <i class="el-icon-question"></i>
            </el-tooltip>
            组件路径
          </span>
          <el-input
            v-model="permission.component"
            placeholder="请输入组件路径"
          />
        </el-form-item>
        <el-form-item v-if="permission.type === 2">
          <el-input
            v-model="permission.permissionValue"
            placeholder="请输入权限标识"
            maxlength="100"
          />
          <span slot="label">
            <el-tooltip
              content="按钮权限，如：('rbac:user:add')"
              placement="top"
            >
              <i class="el-icon-question"></i>
            </el-tooltip>
            权限字符(按钮权限)
          </span>
        </el-form-item>
        <el-form-item label="状态" prop="type">
          <el-radio-group v-model="permission.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          @click="dialogVisible = false"
          size="small"
          icon="el-icon-refresh-right"
          >取 消</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-check"
          @click="addOrUpdate()"
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
  
<script>
//引入permission.js
import permissionApi from "@/api/rbac/permission";

export default {
  name: "",
  data() {
    return {
      permissionVOList: [],

      //添加、编辑相关代码
      dialogVisible: false, //添加、修改dialog
      permission: {}, //添加、修改对应封装的permission对象
      dialogTitle: "",
      typeDisabled: false,
      type0Disabled: false,
      type1Disabled: false,
      type2Disabled: false,
      iconList: [
        {
          class: "el-icon-s-tools",
        },
        {
          class: "el-icon-s-custom",
        },
        {
          class: "el-icon-setting",
        },
        {
          class: "el-icon-user-solid",
        },
        {
          class: "el-icon-s-help",
        },
        {
          class: "el-icon-phone",
        },
        {
          class: "el-icon-s-unfold",
        },
        {
          class: "el-icon-s-operation",
        },
        {
          class: "el-icon-more-outline",
        },
        {
          class: "el-icon-s-check",
        },
        {
          class: "el-icon-tickets",
        },
        {
          class: "el-icon-s-goods",
        },
        {
          class: "el-icon-document-remove",
        },
        {
          class: "el-icon-warning",
        },
        {
          class: "el-icon-warning-outline",
        },
        {
          class: "el-icon-question",
        },
        {
          class: "el-icon-info",
        },
      ],
    };
  },
  created() {
    this.selectAll();
  },
  methods: {
    selectAll() {
      permissionApi.selectAll().then((jsonResult) => {
        console.log(jsonResult);
        this.permissionVOList = jsonResult.data;
      });
    },
    deleteById(id) {
      //弹出确认删除的提示框
      this.$confirm("您确认要删除么?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
      }).then(() => {
        permissionApi.deleteById(id).then((jsonResult) => {
          if (jsonResult.ok) {
            this.$message({
              type: "success",
              message: jsonResult.msg,
            });
            //删除成功之后，查询最新的数据
            this.selectAll();
          } else {
            this.$message({
              type: "error",
              message: jsonResult.msg,
            });
          }
        });
      });
    },
    //添加、编辑相关代码
    showAddDialog(row) {
      this.dialogVisible = true;
      this.permission = {};
      this.dialogTitle = "添加下级菜单";

      if (row) {//点击右侧添加按钮
        this.permission.pid = row.id;
        this.permission.parentName = row.name;
        if (row.type == 0) {
          //在目录下面添加，可以添加：目录、菜单
          this.typeDisabled = false;
          this.type0Disabled = false;
          this.type1Disabled = false;
          this.type2Disabled = true;
        } else if (row.type == 1) {
          //在菜单下面添加：只能添加：按钮
          this.typeDisabled = false;
          this.type2Disabled = false;
          this.type0Disabled = true;
          this.type1Disabled = true;

          this.permission.type = 2;
        }
      } else {
        //添加顶级菜单
        this.dialogTitle = "添加顶级菜单";
        this.typeDisabled = true;
        this.permission.type = 0;
        this.permission.component = "Layout";
        this.permission.pid = 0;
      }
    },
    showUpdateDialog(row) {
      this.dialogVisible = true;
      this.permission = {};
      this.dialogTitle = '更新';
      //this.permission.parentName = row.name;
      permissionApi.selectById(row.id).then((jsonResult) => {
        this.permission = jsonResult.data;
      });
    },
    addOrUpdate() {
      if (!this.permission.id) {
        permissionApi.add(this.permission).then((jsonResult) => {
          if (jsonResult.ok) {
            this.$message({
              type: "success",
              message: jsonResult.msg,
            });
            //关闭弹出框
            this.dialogVisible = false;
            this.selectAll();
          } else {
            this.$message({
              type: "error",
              message: jsonResult.msg,
            });
          }
        });
      } else {
        permissionApi.update(this.permission).then((jsonResult) => {
          if (jsonResult.ok) {
            this.$message({
              type: "success",
              message: jsonResult.msg,
            });
            this.dialogVisible = false;
            this.selectAll();
          } else {
            this.$message({
              type: "error",
              message: jsonResult.msg,
            });
          }
        });
      }
    },
  },
};
</script>
 
<style scoped>
</style>